package org.van.financial.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.van.financial.model.SecurityDomain;
import org.van.financial.service.security.SecurityService;
import org.van.financial.utils.APIResponse;
import org.van.financial.utils.GsonUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/api")
public class SecurityController extends BaseController{

    @Autowired
    private SecurityService securityService;

    @PostMapping(value = "/security")
    @ResponseBody
    public String getSecurityList(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(name = "security_name" , required = true) String security_name) {
        try {
            List<SecurityDomain> securities = securityService.getSecurityByCond(security_name);
            return GsonUtils.toJsonString(securities);
        } catch (Exception e) {
            //LOGGER.error(e.getMessage());
        }
        return "Error Get Securities";
    }

    @PostMapping(value = "/addsecurity")
    @ResponseBody
    public APIResponse<?> addSecurity(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(name = "security_name" , required = true) String security_name,
            @RequestParam(name = "security_number" , required = true) String security_number,
            @RequestParam(name = "security_code" , required = true) String security_code,
            @RequestParam(name = "security_amount" , required = true) Integer security_amount,
            @RequestParam(name = "security_price" , required = true) Integer security_price,
            @RequestParam(name = "security_time" , required = true) String security_time,
            @RequestParam(name = "security_direction" , required = true) String security_direction,
            @RequestParam(name = "security_notes" , required = false) String security_notes){
        SecurityDomain security = new SecurityDomain();
        security.setSecurity_name(security_name);
        security.setAccount_number(security_number);
        security.setSecurity_code(security_code);
        security.setSecurity_amount(security_amount);
        security.setSecurity_price(security_price);
        security.setSecurity_time(security_time);
        security.setSecurity_direction(security_direction);
        if (null != security_notes) security.setSecurity_notes(security_notes);
        // try {
            securityService.addNewSecurity(security);
        //} catch (Exception e){
        //    return APIResponse.fail("");
        //}
        return APIResponse.success();
    }

    @PostMapping(value = "/deletesecurity")
    @ResponseBody
    public APIResponse<?> deleteSecurity(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(name = "uuid" , required = true) int uuid){
        try {
            securityService.deleteSecurityById(uuid);
        } catch (Exception e){
            return APIResponse.fail("");
        }
        return APIResponse.success();
    }
}
